import React from 'react';

import './static/css/login.css';

import $ from 'jquery';
import axios from 'axios';
import {browserHistory,Link} from 'react-router';

class Login extends React.Component{
    constructor(props){
        super(props);
    }
    componentDidMount(){
        $(".username").val(localStorage.getItem("tel"));
        $(".password").val(localStorage.getItem("pwd"));
        var Icon = $(".icon").get(0);
        var InputNode = $(".password").get(0);
   
        $(".icon").click(function () {
            if ($(InputNode).attr("type") == "password") {
                $(InputNode).attr("type","text")
                $(Icon).removeClass("icon-yanjing1").addClass("icon-yanjing");
            } else {
                $(InputNode).attr("type","password")
                $(Icon).removeClass("icon-yanjing").addClass("icon-yanjing1");
                }
        })

        $(".submitBtn").click(function(){
            axios.post('http://127.0.0.1:2000/api/login',{tel:$(".username").val(),password:$(".password").val()})
            .then(function (response) {
               if(response.data.success==1){
                    sessionStorage.setItem("user_info",JSON.stringify(response.data.result[0]));
                    alert("登录成功");
                    browserHistory.push('UserName');
               }
                
            })
            .catch(function (error) {
                console.log(error);
            });  
        })
    }
    render(){
        return(
        <div className="logins">
        <header className="header7">
            <a href=""><i className="iconfont">&#xe607;</i></a> 登录
        </header>
        <div className="formbox">
            <div>
                <input type="text" placeholder="手机/邮箱/用户名" className="username"/>
            </div>
            <div>
                <input type="password" placeholder="密码" className="password"/>
                <i className="iconfont icon icon-yanjing1"></i>
           </div>
        </div>
        <button className="submitBtn">登录</button>
        <div className="forgetps">
            没有账号，请先
                <Link to="/Register1">注册</Link>
            <a href="javascript:;">忘记密码？</a>
        </div>
        <div className="alertbox"></div>
        </div>
        )
    }
        
}

export default Login;